/******************************************************************************
*filename: breakfileTesting.c
*part-size = 1MB.(pieces) temp for testing on video file.
*************** BESURE TO KEEP VIDEO SIZE LIMITED TO AROUND 10 MB OR IT MIGHT CREATE MEM PROBLEMS******8
ALSO DELETe the parts by hand. This program doesnt do that
* This program needs a file named testvideo.flv or something then creates parts and then joinss to form Newtestvideo.flv or something.
*Author : Ashwin raut
*Date:14th Nov 2010
*******************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define PARTSIZE 1048576
#define PARTSIZE 65536

void breakfile(char * filename,long int fileSize,int noOfParts,int lpsize)
{
	FILE *tempfp;
	FILE *fp;
	char newfilename[20];
	char * buffer;
	//int piecesize=65535;
	size_t n;
	int i,Partno;
	fp=fopen(filename,"rb+");
	 buffer = (char*) malloc (sizeof(char)*PARTSIZE);
	 
	 
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
	printf("\nBreaking file: %s",filename);
	if(lpsize!=0){
	
		for(i=1;i<noOfParts;i++){
		memset(buffer,0,sizeof(buffer));
		sprintf(newfilename,"%s.part%d",filename,i);
		printf("\nCreating part file %s",newfilename);
		tempfp=fopen(newfilename,"wb+");
		n=fread(buffer,1,PARTSIZE,fp);
		fwrite(buffer,1,n,tempfp);
		fclose(tempfp);
		}
		memset(buffer,0,sizeof(buffer));
		sprintf(newfilename,"%s.part%d",filename,noOfParts);
		printf("\n&Creating part file %s",newfilename);
		tempfp=fopen(newfilename,"wb+");
		n=fread(buffer,1,lpsize,fp);
		fwrite(buffer,1,lpsize,tempfp);
		fclose(tempfp);
		fclose(fp);
	}
	else{
	
		sprintf(newfilename,"%s.part%d",filename,noOfParts);
		printf("\n*Creating part file %s",newfilename);
		tempfp=fopen(newfilename,"wb+");
		n=fread(buffer,1,fileSize,fp);
		fwrite(buffer,1,fileSize,tempfp);
		fclose(tempfp);
		fclose(fp);
	
	
	}
	free(buffer);
}
void joinfile(char * filename,long int fileSize,int noOfParts,int lpsize)
{
	FILE *tempfp;
	FILE *fp;
	char newfilename[20];
	char appendedfile[20];
	char * buffer;
	//int piecesize=65535;
	size_t n;
	int i,Partno;
	sprintf(appendedfile,"%s%s","New",filename);
	fp=fopen(appendedfile,"a+");
	 buffer = (char*) malloc (sizeof(char)*PARTSIZE);
	 if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
	printf("\n****Appending parts and forming: %s****",appendedfile);
	if(lpsize!=0){
	
		for(i=1;i<noOfParts;i++){
		memset(buffer,0,sizeof(buffer));
		sprintf(newfilename,"%s.part%d",filename,i);
		printf("\nAppending %s to file %s",newfilename,appendedfile);
		tempfp=fopen(newfilename,"r+");
		n=fread(buffer,1,PARTSIZE,tempfp);
		fwrite(buffer,1,n,fp);
		fclose(tempfp);
		}
		memset(buffer,0,sizeof(buffer));
		sprintf(newfilename,"%s.part%d",filename,noOfParts);
		printf("\nAppending %s to file %s\n",newfilename,appendedfile);
		tempfp=fopen(newfilename,"r+");
		n=fread(buffer,1,lpsize,tempfp);
		fwrite(buffer,1,lpsize,fp);
		fclose(tempfp);
		fclose(fp);
	}
	else{
	
		sprintf(newfilename,"%s.part%d",filename,noOfParts);
		printf("\nAppending %s to file %s\n",newfilename,appendedfile);
		tempfp=fopen(newfilename,"r+");
		n=fread(buffer,1,fileSize,tempfp);
		fwrite(buffer,1,fileSize,fp);
		fclose(tempfp);
		fclose(fp);
	
	
	}
free(buffer);

}


int main()
{
	FILE *fp;
	long int offset;
	long int fileSize;
	int noOfParts=0;
	int lpsize=0;
	char * filename="photo.jpg";
	struct file_part *filehead;
	
	fp=fopen(filename,"rb+");
	fseek (fp,0,SEEK_END);
  	fileSize = ftell(fp);
  	rewind (fp);
	
	noOfParts= (int) fileSize/PARTSIZE;
	lpsize=(int) fileSize%PARTSIZE;
	
	if(lpsize==0)
	{
		//do nothing
	}
	else
	{
		noOfParts=noOfParts+1;
	}
	printf("The No of parts is: %d\nlast part size is: %d",noOfParts,lpsize);
	breakfile(filename,fileSize,noOfParts,lpsize);	
	joinfile(filename,fileSize,noOfParts,lpsize);
	fclose(fp);	
return 0;
}
